package edu.pxd.cecs.ece510.pocketmanager;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;

import android.app.Activity;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.TextView;

public class ShowexpenseActivity extends Activity {

	public static final String EVENT_POSITION_KEY = "edu.pxd.cecs.ece510.pocketmanager.event_position";
	public static final String EVENT_KEY = "edu.pxd.cecs.ece510.pocketmanager.event_position";

	private String EventTable = "EventTable";
	
	private ListView balanseList;
	private Button editButton;
	private Button continueButton;
	private TextView totalText;
	
	private ArrayList<String> balanseArr;
	private ArrayAdapter<String> listadapter;
	
	String fetchEvent;	
	
	
	private int getPosition;
	private float getTotal;
	private int listcnt;
	
	private String[] getName;
	private float[] getAmount;
	private float[] getBalanse;
	private String[] strFill;
	private Float[] sortedData;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_showexpense);
		
		getPosition = getIntent().getIntExtra(EVENT_POSITION_KEY,0);
		Log.d("GetPosition", String.valueOf(getPosition));
		
		
		
		
		
		
		
		//getTotal = getIntent().getFloatExtra(EVENT_KEY,0);
		//Log.d("GetTotal", String.valueOf(getTotal));
		
		continueButton = (Button)findViewById(R.id.newcontinuebutton);
		editButton = (Button)findViewById(R.id.neweditbutton);
		balanseList=(ListView)findViewById(R.id.listViewbalanse);
		totalText=(TextView)findViewById(R.id.totalamount);
		
		balanseArr = new ArrayList<String>();		
		listadapter = new ArrayAdapter<String>(getApplicationContext(), android.R.layout.simple_list_item_1 ,balanseArr);
		balanseList.setAdapter(listadapter);
		
		Cursor seeEvent = CreateActivity.db.rawQuery("SELECT * from " + EventTable, null);
		int count2 = seeEvent.getCount();
		Log.d("Count", String.valueOf(count2));
		seeEvent.moveToPosition((getPosition-1));
		
		//totalText.setText(String.valueOf(getTotal));
		
		fetchEvent = seeEvent.getString(seeEvent.getColumnIndex("eventname")).replaceAll(" ", ""); 
		Log.d("EVENT", fetchEvent);
		
		Cursor nameLook = CreateActivity.db.rawQuery("SELECT * from " + fetchEvent, null);
		listcnt = nameLook.getCount();
		nameLook.moveToFirst();
		getName = new String[listcnt];
		getAmount = new float [listcnt];
		getBalanse = new float[listcnt];
		strFill = new String[listcnt]; 
		sortedData=new Float[listcnt];
		for(int track1 = 0;track1 < listcnt; track1++)
		{
			String temp1 = nameLook.getString(nameLook.getColumnIndex("personeName"));
			Log.d("NAME", temp1);
			getName[track1] = temp1;
			getAmount[track1] = nameLook.getFloat(nameLook.getColumnIndex("amountPay"));
			getAmount[track1] = (float)(Math.round(getAmount[track1]*100.0)/100.0);
			getTotal =   getTotal + getAmount[track1];
			getTotal = (float)(Math.round(getTotal*100.0)/100.0);
			Log.d("GetTotal", String.valueOf(getTotal));
			
			getBalanse[track1] = nameLook.getFloat(nameLook.getColumnIndex("calculatedAmount"));
			getBalanse[track1] = (float)(Math.round(getBalanse[track1]*100.0)/100.0);
			sortedData[track1] =getBalanse[track1];
			
			Log.d("GetName", getName[track1]);
			Log.d("GetAmount", String.valueOf(getAmount[track1]));
			Log.d("Getbalanse", String.valueOf(getBalanse[track1]));
			nameLook.moveToNext();
		}
		totalText.setText(String.valueOf(getTotal));
		
		for(int track1 = 0;track1 < listcnt; track1++)
		{
			float tempBalanse = Math.abs(getBalanse[track1]);
			Log.d("TempBalanse", String.valueOf(tempBalanse));
			if((getBalanse[track1]) < 0)
			{
				strFill[track1] = getName[track1] + " Owes "+ tempBalanse ;
			}
			else
			{
				strFill[track1] = getName[track1] + " Is Owed "+ tempBalanse;
			}
			Log.d("StrFill", strFill[track1]);
			balanseArr.add(strFill[track1]);
			listadapter.notifyDataSetChanged();
		}
		
		
		
		
		
		
		
		
		Arrays.sort(sortedData,Collections.reverseOrder());
		for(int track1 = 0;track1 < listcnt; track1++)
		{
			Log.d("sortedData", String.valueOf(sortedData[track1]));
		}
		editButton.setOnClickListener(new View.OnClickListener() {
		    
		    @Override
			public void onClick(View v) {
		    	Intent i = new Intent(ShowexpenseActivity.this, AddexpenseActivity.class);
				i.putExtra(AddexpenseActivity.EVENT_GET_POSITION_KEY,getPosition);
		    	startActivity(i);
		    	finish();
		    }
		});
		continueButton.setOnClickListener(new View.OnClickListener() {
		    
		    @Override
			public void onClick(View v) {
		    	Intent i = new Intent(ShowexpenseActivity.this, CreateActivity.class);
				startActivity(i);
		    	finish();
		    }
		});
	}
	
}
